#pragma once
#include "cocos2d.h"
#include "GameConfig.h"
#include "EventHelper.h"
#include "UI.h"

USING_NS_CC;

namespace uicommon
{
	enum UIComponent
	{
		UIC_INPUT,
		UIC_DROPDOWNLIST,
		UIC_SWITCH,
		UIC_SLIDEBAR
	};
};

class UICDropdownList :public ClippingNode, public EventHelper
{
	public:
	UICDropdownList() :EventHelper(this) {};
	static UICDropdownList* create(
		Size size,
		Color4B bgcolor,
		Color4B fgcolor,
		TextHAlignment align = TextHAlignment::CENTER
	);

	Size size;
	Color4B bgcolor, fgcolor, hvcolor;
	int itemcount = 0;
	int selectedindex = 0;
	int hoverindex = -1;
	int zorder;

	void addItem(string text, int tag = 0);
	int getHoverIndex(float posy);
	int getSelectedTag();
	void setSelectedIndex(int index);

	private:
	vector<LayerColor*> items;
	LayerColor* cliplayer;
	LayerColor* itemspace;
	TextHAlignment align;

	bool isExpanded = false;

	void onMouseMove(Event* e);
	void onMouseDown(Event* e);
};

class UICSliderBar :public LayerColor, public EventHelper
{
	public:
	UICSliderBar() :EventHelper(this) {};
	static UICSliderBar* create(
		Size size,
		Color4B bgcolor,
		Color4B fgcolor,
		float minval = 0,
		float maxval = 100,
		float steppingval = 0,
		float defvalue = 0,
		int valdecimal = 0
	);

	float getValue();
	void setValue(float newvalue, bool setptrloc = false);
	void setValueByLoc(float locx, bool setptrloc = false);
	void setOnValueChangeAction(function<void()> func);

	private:
	float value, defvalue, valdecimal;
	Size size;
	Color4B bgcolor, fgcolor;
	float minval, maxval, steppingval;
	float ptrminx, ptrmaxx, ptrspace;
	bool isdragging = false;
	LayerColor* pointer;
	Label* valuetext;
	function<void()> onValueChangeAction = nullptr;

	float sorbPtrInRange(float x);
	void setValueByMouse(Event* e);

	void onMouseMove(Event* e);
	void onMouseDown(Event* e);
	void onMouseUp(Event* e);
};

class UICSwitchOnOff :public ClippingNode, public EventHelper
{
	public:
	UICSwitchOnOff() :EventHelper(this) {};
	static UICSwitchOnOff* create(
		Size size,
		Color4B bgcolor,
		Color4B fgcolor,
		bool defison = true,
		string texton = "ON",
		string textoff = "OFF",
		TextHAlignment align = TextHAlignment::CENTER
	);


	void toggleStatus();
	void setStatus(bool isStatusOn);
	void updateUI();
	bool getStatus();

	private:
	Size size;
	Color4B bgcolor, fgcolor, hvcolor;
	bool isOn = true;
	TextHAlignment align;
	LayerColor* itemspace;

	void onMouseMove(Event* e);
	void onMouseDown(Event* e);
};

class UICButton :public LayerColor, public EventHelper
{
	public:
	UICButton() :EventHelper(this) {};
	static UICButton* create(
		Size size,
		string text,
		Color4B bgcolor,
		Color4B fgcolor,
		TextHAlignment align = TextHAlignment::CENTER
	);

	public:
	enum DIRECTION
	{
		UP,
		LEFT,
		RIGHT
	};

	void setActiveStat(bool active);
	void toggleActiveStat();
	bool getActiveStat();
	void setTag(int tagvalue);
	int getTag();
	void setDirection(DIRECTION direction);

	private:
	Size size;
	Color4B bgcolor, fgcolor, hvcolor;
	bool isActive = false;
	int tag = 0;
	DIRECTION direction = UP;
	LayerColor* underline;
	Label* text;

	void onMouseMove(Event* e);
	void onMouseDown(Event* e);
};